﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace TimKen.Web.Admin.Resource
{
    public partial class Resource : TimKen.App.AdminPage
    {
        private TimKen.BussLogic.ResourceManager rm = new TimKen.BussLogic.ResourceManager();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindResources();
                Bind();
            }
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            string filename = txtFileName.Text.Trim().ToLower();
            string desCN = txtDescripCN.Text;
            string desEN = txtDescripEN.Text;
            TimKen.DataEntity.Resource r = new TimKen.DataEntity.Resource();
            r.FileName = filename;
            r.DescriptCN = desCN;
            r.DescriptEN = desEN;
            r.Enname = txtEnName.Text;
            r.Route = "";
            r.Filesize = "10";
            if (rm.Exists(filename))
            {
                if (Ban_Img.FileName != "")
                {
                    string fullname = this.Ban_Img.FileName;
                    string typ2 = fullname.Substring(fullname.LastIndexOf(".") + 1);
                    if (this.Ban_Img.FileName != null && this.Ban_Img.FileName != "")   // && typ2 == "gif" || typ2 == "jpg" || typ2 == "bmp" || typ2 == "png"
                    {
                        System.IO.File.Delete(Server.MapPath("~/upload/resource/" + ViewState["path"].ToString()));
                    }
                    string file = UpFiles(this.Ban_Img);
                    if (file != "")
                    {
                        r.Route = file;
                    }
                }
                else
                    r.Route = txt_URL.Text;
                rm.Update(r);
            }
            else
            {
                if (Ban_Img.FileName != "")
                {
                    string file = UpFiles(this.Ban_Img);
                    if (file != "")
                    {
                        r.Route = file;
                    }
                }
                else
                    r.Route = txt_URL.Text;
                rm.Add(r);
            }
            Alert(GetLangString("SaveSuccessful"));
            Bind();
        }
        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="fileuploadname"></param>
        /// <returns></returns>
        public string UpFiles(FileUpload fileuploadname)
        {
            string fullname = fileuploadname.FileName;//这个属性是以前2003没有的，也许是我没注意看，反正现在我才第一次用！直接取得文件名
            string url = fileuploadname.PostedFile.FileName;//这个是以前2003用的，先取得全部的上传文件路径个名字，然后再利用SubString方法来得到用户名，现在看来是没有必要了
            string typ = fileuploadname.PostedFile.ContentType;//获取文件MIME内容类型
            string typ2 = fullname.Substring(fullname.LastIndexOf(".") + 1);//获取文件名字 . 后面的字符作为文件类型
            string size = fileuploadname.PostedFile.ContentLength.ToString();
            //string date = DateTime.Now.ToString("yyyyMMddHHmmss");
            string filename = "";
            //下面是保存了，我们来个判断，只能上穿传那些文件吧
            //if (typ2 == "gif" || typ2 == "jpg" || typ2 == "bmp" || typ2 == "png")
            //{
            filename = fullname;
            fileuploadname.SaveAs(String.Format("{0}/{1}", Server.MapPath("~/upload/resource"), filename));//将文件保存在跟目录的UP文件夹下
            //}
            //else
            //{
            //    Response.Write("<script>alert('文件类型不正确!');</script>");
            //}
            return filename;
        }

        protected void Bind()
        {
            DataSet ds = rm.GetPageList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, "");
            gvFileList.DataSource = ds.Tables[0].DefaultView;
            gvFileList.DataBind();
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0]["total"]);
            txtFileName.Text = "";
            txtDescripCN.Text = "";
            txtDescripEN.Text = "";
            txtEnName.Text = "";
            txt_URL.Text = "";
            ViewState["path"] = "";// dt.Rows[0]["ImagePath"].ToString();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            Bind();
        }

        protected void gvFileList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                rm.Delete(e.CommandArgument.ToString());
                Bind();
            }
            else if (e.CommandName == "mod")
            {
                TimKen.DataEntity.Resource r = rm.GetModel(e.CommandArgument.ToString());
                txtFileName.Text = r.FileName;
                txtDescripCN.Text = r.DescriptCN;
                txtDescripEN.Text = r.DescriptEN;
                txtEnName.Text = r.Enname;
                txt_URL.Text = r.Route;
                ViewState["path"] = r.Route;// dt.Rows[0]["ImagePath"].ToString();
            }
        }

        protected void BindResources()
        {
            btnSave.Text = GetLangString("Save");
            gvFileList.Columns[0].HeaderText = GetLangString("FileName");
            gvFileList.Columns[1].HeaderText = GetLangString("DescriptionCN");
            gvFileList.Columns[2].HeaderText = GetLangString("ENName");
            gvFileList.Columns[3].HeaderText = GetLangString("DescriptionEN");
            gvFileList.Columns[4].HeaderText = GetLangString("FileSize");
        }
    }
}